package com.zengqingfa.springboot.freemarker.demo.controller;

import com.zengqingfa.springboot.freemarker.demo.entity.DbColumnType;
import com.zengqingfa.springboot.freemarker.demo.entity.TableField;
import com.zengqingfa.springboot.freemarker.demo.entity.TableInfo;
import com.zengqingfa.springboot.freemarker.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @author zengqingfa
 * @date 2019/11/20 14:20
 * @description
 * @email zengqingfa_java@163.com
 */
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {

    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("name", "zengqingfa");
        model.addAttribute("email", "zengqingfa_java@163.com");
        TableInfo tableInfo = setFields();
        model.addAttribute("tableInfo", tableInfo);
        return "index";
    }

    /**
     * 封装list参数
     *
     * @return
     */
    public TableInfo setFields() {
        TableInfo tableInfo = new TableInfo();
        List<TableField> list = new ArrayList<>();
        TableField tableField = new TableField();
        tableField.setComment("主键");
        tableField.setName("id");
        tableField.setType("bigint(20) unsigned");
        tableField.setPropertyName("id");
        tableField.setColumnType(DbColumnType.STRING);
        list.add(tableField);
        tableInfo.setFields(list);
        return tableInfo;
    }

    @RequestMapping("/f01")
    public String f01(Model model) {
        model.addAttribute("flag", true);

        model.addAttribute("createTime", new Date());

        model.addAttribute("age", 22);
        model.addAttribute("salary", 1000);
        model.addAttribute("avg", 5.2456);


        model.addAttribute("msg", "hello");
        model.addAttribute("msg2", "world");

        String[] strArr = new String[]{"刘德华", "黎明", "郭富城", "张学友"};
        model.addAttribute("strArr", strArr);

        List<String> strList = Arrays.asList(strArr);
        model.addAttribute("strList", strList);

        User user1 = new User("zengqingfa", 30);
        User user2 = new User("laish", 27);
        User user3 = new User("xxli", 29);
        List<User> users = Arrays.asList(user1, user2, user3);
        model.addAttribute("users", users);

        Map<String, String> citys = new HashMap<>();
        citys.put("city1", "深圳");
        citys.put("city2", "上海");
        citys.put("city3", "广州");
        citys.put("city4", "北京");
        model.addAttribute("citys", citys);
        return "f01";
    }

    @RequestMapping("/f02")
    public String f02(Model model) {
        return "f02";
    }

    @RequestMapping("/f03")
    public String f03(Model model) {
        return "f03";
    }

    @RequestMapping("/f04")
    public String f04(Model model) {
        return "f04";
    }


}
